<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="dao.*,dao.impl.*,entity.*" %>
<%@ page import="java.util.List" %>

<%
    TopicDao topicDao = new TopicDaoImpl();		//得到主题Dao的实例
    ReplyDao replyDao = new ReplyDaoImpl(); 	//得到回复Dao的实例
    UserDao userDao = new UserDaoImpl(); 		//得到用户Dao的实例
    BoardDao boardDao = new BoardDaoImpl(); 	//得到板块Dao的实例
    int boardID = Integer.parseInt(request.getParameter("boardID")); 	//取得板块ID
    int topicID = Integer.parseInt(request.getParameter("topicID")); 	//取得主题ID
    int p = Integer.parseInt(request.getParameter("page"));			//取得页码
    Board board = boardDao.findBoard(boardID); 					//取得板块信息
    Topic topic = topicDao.findTopic(topicID); 	//取得主题信息
    User topicUser = userDao.findUser(topic.getUid());  //取得主题作者
    List listReply = replyDao.findListReply(p, topicID);	//取得该主题的回复列表

    int prep = p;	//上一页
    int nextp = p;	//下一页
    if(listReply.size() == 10){
        nextp = p+1;
    }
    if(p > 1){
        prep = p - 1;
    }
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>主页</title>
    <link rel="stylesheet" href="css/topbar.css">
    <link rel="stylesheet" href="css/index.css">
</head>
<body>
<div class="main">
    <div class="top">
        <label>校园BBS系统</label>
    </div>
    <%
        if(session.getAttribute("user") == null){
    %>
    <div class="bar"><p>您尚未
        <a href="login.jsp" target="_blank">登录</a>&nbsp;|
        <a href="register.jsp" target="_blank">注册</a>
    </p>
    </div>
    <%
    }
    else{
        User loginUser = (User)session.getAttribute("user");
    %>
    <p class ="bar">
        您好：<%= loginUser.getUName() %>&nbsp;|&nbsp;
        <a href ="login.jsp" target="_blank">登出</a>
    </p>
    <%	} %>
    <div class="crumbs" style="padding-left: 150px;">
        	<span class="layui-breadcrumb">
  				<a href="index.jsp"><i class="layui-icon layui-icon-home"></i>&nbsp;论坛主页&nbsp;/</a>
<%--  				<a href=""><i class="layui-icon layui-icon-read"></i>&nbsp;灌水乐园</a>--%>
                <a href ="ListServlet?page=1&boardID=<%= boardID %>"><i class="layui-icon layui-icon-read"></i>&nbsp;<%= board.getBoardName() %>&nbsp;/</a>
  				<a><cite><%= topic.getTitle() %></cite></a>
                      <div style="padding-left: 500px;" >
        <a href="detail.jsp?page=<%=prep %>&boardID=<%=boardID%>&topicID=<%=topicID %>">上一页</a>&nbsp;|
        <a href="detail.jsp?page=<%=nextp%>&boardID=<%=boardID%>&topicID=<%=topicID  %>">下一页</a></div>
			</span>
    </div>

    <div class="table-style">
        <!--  	主题 	 -->
        <%
            if(p == 1){
        %>
        <table id="table-set">
            <!-- 	本页主题的标题	 -->
            <thead>
            <th><a href ="ReplyServlet?topicID=<%= topicID %>&boardID=<%= boardID %>" target="_blank">回复帖子</a>&emsp;
                <a href ="PostServlet?boardID=<%= boardID %>" target="_blank">发表话题</a>
            </th>
            <th colspan="1" align="left">本页主题：<%= topic.getTitle() %></th>
            </thead>
            <tr>
                <td style="Width:20%" rowspan="3">
                    <b><%= topicUser.getUName() %></b><br/>
                    <img src="images/<%= topicUser.getHead()%>" alt ="" style="width: 100px;height: 100px;border-radius: 50px;"/><br/>
                    注册时间：<%= topicUser.getRegTime() %>
                </td>
                <td><h4 align="left">标题:&emsp;<%= topic.getTitle() %></h4></td>
            </tr>
            <tr>
                <td><h4 align="left">内容:&emsp;<%= topic.getContent() %></h4></td>
            </tr>
            <tr>
                <td><h4 align="left">时间:&emsp;发表：[<%= topic.getPublishTime()%>]&nbsp;
                    最后修改：[<%= topic.getModifyTime() %>]
                </h4></td>
            </tr>
        </table>
        <!--   回复	 -->
        <%
            }
            for(int i = 0 ; i<listReply.size(); i++){
                Reply reply = (Reply)listReply.get(i);  //循环取得回复信息
                User replyUser =(User)userDao.findUser(reply.getUid()); //取得回复的作者

        %>
        <div class="bor">
            <table width="100%" id="table-set">
                <tr>
                    <td style="width:20%" rowspan="3">
                        <b><%= replyUser.getUName() %></b><br/>
                        <img src="images/<%= replyUser.getHead()%>" alt ="" style="width: 100px;height: 100px;border-radius: 50px;"/><br/>
                        注册：<%= topicUser.getRegTime() %>
                    </td>
                    <td><h4 align="left"><%= reply.getTitle() %></h4></td>
                </tr>
                <tr>
                    <td><h4 align="left"><%= reply.getContent() %></h4></td>
                </tr>
                <tr>
                    <td><h4 align="left">发表：[<%= reply.getPublishTime()%>]&nbsp;
                        最后修改：[<%= topic.getModifyTime() %>]
                        <a href="DeleteReplyServlet?page=<%= p %>&replyID=<%= reply.getReplyId() %>&boardID=<%= boardID%>&topicID=<%= topicID%>">[删除]</a>
                        <a href="UpdateReplyServlet?replyID=<%= reply.getReplyId() %>&boardID=<%= boardID%>&topicID=<%= topicID%>">[修改]</a>
                    </h4></td>
                </tr>
            </table>
        </div>
        <br/>
        <%
            }
        %>

</div>
</div>
</body>
</html>